package com.fzdy.product.controller.admin;

import com.fzdy.product.base.AjaxCallBack;
import com.fzdy.product.base.vo.FrontPage;
import com.fzdy.product.controller.api.CopyProcessApi;
import com.fzdy.product.controller.api.OrderApi;
import com.fzdy.product.dao.UserDao;
import com.fzdy.product.entity.Controller;
import com.fzdy.product.entity.Order;
import com.fzdy.product.entity.ProductType;
import com.fzdy.product.service.ControllerService;
import com.fzdy.product.service.OrderService;
import com.fzdy.product.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private UserDao userDao;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private ControllerService controllerService;

    @RequestMapping(value = "/getOrderList")
    public AjaxCallBack getOrderList(@RequestBody FrontPage<OrderApi> frontPages) {
        return new AjaxCallBack(orderService.getOrdersByPage(frontPages));
    }

    @RequestMapping(value = "/checkSN")
    public AjaxCallBack checkSN(@RequestBody OrderApi api) {
        return orderService.checkSnRange(api) ? new AjaxCallBack("200", "SN范围可用") : new AjaxCallBack("300", "SN范围存在已有设备");
    }


    @RequestMapping(value = "/addOrder")
    public AjaxCallBack addOrder(@RequestBody OrderApi api) {
        if (orderService.checkSnRange(api)) {
            return new AjaxCallBack("300", "SN范围不可用");
        }
        try {
            return orderService.insertOrder(api)?new AjaxCallBack("200", "添加成功"):new AjaxCallBack("300", "订单号已被使用");
            //TODO 发给控制器
        } catch (Exception ex) {
            return new AjaxCallBack("300", "添加失败");
        }
    }

    @RequestMapping(value = "/getTypeList")
    public List<ProductType> getTypeList() {
        return productTypeService.getAll();
    }

    @RequestMapping(value = "/getControllerList")
    public List<Controller> getControllerList() {
        return controllerService.getAll();
    }

    @RequestMapping(value = "/addProduct")
    public AjaxCallBack addProduct(@RequestBody ProductType type) {
        return productTypeService.save(type) ? new AjaxCallBack("200", "添加成功") : new AjaxCallBack("300", "添加失败");
    }

    @RequestMapping(value = "/copyProcess")
    public AjaxCallBack copyProcess(@RequestBody CopyProcessApi api) {
        orderService.copyProcess(api);
        return null;
    }

    @RequestMapping(value = "/updateNum")
    public AjaxCallBack updateNum(@RequestBody OrderApi api) {
        Order order=orderService.getOrderByOrderId(api.getOrderId());
        if (order!=null){
            if (order.getDeviceNum()<api.getDeviceNum()){
                return new AjaxCallBack("300", "要增加设备数，请添加新订单");
            }
            order.setNewNum(api.getDeviceNum());
            orderService.updateNum(order);
            return new AjaxCallBack("200", "修改成功");
        }else {
            return new AjaxCallBack("300", "订单不存在");
        }
    }
}
